//
//  RateViewController.m
//  预购清单
//
//  Created by  on 2022/10/16.
//

#import "RateViewController.h"
#import "RateCell.h"
#import "InputAlertController.h"

@interface RateViewController ()
@property (nonatomic,strong) NSArray *array;
@property (nonatomic,strong) NSArray *imgs;
@property (nonatomic,strong) NSArray *sort;
@property (nonatomic,strong) NSArray *p;
@property (nonatomic,assign) CGFloat num;

@end

@implementation RateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
- (void)initUI
{
    self.title = LS(@"汇率计算");
    self.array = @[@"人民币CNY",@"美元USD",@"港元HKD",@"澳门元MOP",@"欧元EUR",@"英镑GBP",@"新台币TWD",@"日元JPY"];
    self.imgs = @[@"CN",@"US",@"HK",@"MC",@"EU",@"GB",@"TW",@"JP"];
    self.sort = @[@"¥",@"$",@"HK$",@"MOP$",@"€",@"£",@"NT$",@"JP¥"];
    self.p = @[@"7.191",@"1",@"7.8498",@"8.077",@"1.0259",@"0.8901",@"32",@"148.59"];

    self.tableViewStyle = UITableViewStyleGrouped;
    self.tableView.layer.masksToBounds = NO;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerNib:[RateCell nib] forCellReuseIdentifier:[RateCell ID]];
    self.tableView.c_top.equalTo(self.view.c_top).coffset(16);
    self.tableView.c_left.equalTo(self.view.c_left).coffset(15);
    self.tableView.c_right.equalTo(self.view.c_right).coffset(-15);
    self.tableView.c_bottom.equalTo(self.view.c_bottom).coffset(-15);
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RateCell *cell = [tableView dequeueReusableCellWithIdentifier:[RateCell ID]];
    cell.name.text = LS(self.array[indexPath.row]);
    cell.flag.image = [UIImage imageNamed:self.imgs[indexPath.row]];
    cell.type.text = LS(self.sort[indexPath.row]);
    cell.num.text = [NSString stringWithFormat:@"%0.2f",self.num * [self.p[indexPath.row] floatValue]];
    return  cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    InputAlertController *vc = [InputAlertController new];
    vc.input.keyboardType = UIKeyboardTypeNumberPad;
    vc.input.textAlignment = NSTextAlignmentCenter;
    vc.input.placeholder = LS(@"请输入金额");
    vc.titleLbl.text = LS(self.array[indexPath.row]);
    vc.block = ^(NSString * _Nonnull str) {
        self.num = [str intValue] / [self.p[indexPath.row] floatValue];
        [tableView reloadData];
    };
    [self presentViewController:vc animated:NO completion:nil];
}

@end

